//
//  UIView+Extension.m
//  SinaWeibo
//
//  Created by chensir on 15/10/13.
//  Copyright (c) 2015年 ZT. All rights reserved.
//

#import "UIView+Extension.h"

@implementation UIView (Extension)

+ (void)load {
    
    [super load];

}

- (instancetype)initAAWithFrame:(CGRect)frame {
    
    return [self initAAWithFrame:[self AAGetNewFrame:frame]];
}

- (void)setAAFrame:(CGRect)frame {
    
    [self setAAFrame:[self AAGetNewFrame:frame]];
}

- (void)setAABounds:(CGRect)bounds {
    
    [self setAABounds:[self AAGetNewFrame:bounds]];
}

- (void)sizeAAToFit {
    
    if ([NSStringFromClass([self class]) isEqualToString:@"UIButton"]) {
        UIButton * button = (UIButton *)self;
        NSLog(@"===005 %@ %lf", button.titleLabel.text, button.wb_width);
    }
    if ([NSStringFromClass([self class]) isEqualToString:@"UILabel"]) {
        UILabel * label = (UILabel *)self;
        NSLog(@"===006 %@ %lf", label.text, label.wb_width);
    }
    [self sizeAAToFit];
}

- (CGRect)AAGetNewFrame:(CGRect)nanFrame {
    
    if (isnan(nanFrame.origin.x)) {
        nanFrame.origin.x = 0.f;
        NSLog(@"===001 %lf %lf %lf %lf %@", nanFrame.origin.x, nanFrame.origin.y, nanFrame.size.width, nanFrame.size.height, NSStringFromClass([self class]));
    }
    if (isnan(nanFrame.origin.y)) {
        nanFrame.origin.y = 0.f;
        NSLog(@"===002 %lf %lf %lf %lf %@", nanFrame.origin.x, nanFrame.origin.y, nanFrame.size.width, nanFrame.size.height, NSStringFromClass([self class]));
    }
    if (isnan(nanFrame.size.width)) {
        nanFrame.size.width = 0.f;
        NSLog(@"===003 %lf %lf %lf %lf %@", nanFrame.origin.x, nanFrame.origin.y, nanFrame.size.width, nanFrame.size.height, NSStringFromClass([self class]));
        if ([NSStringFromClass([self class]) isEqualToString:@"UIButton"]) {
            UIButton * button = (UIButton *)self;
            NSLog(@"===007 %@ %lf", button.titleLabel.text, button.wb_width);
        }
        if ([NSStringFromClass([self class]) isEqualToString:@"UILabel"]) {
            UILabel * label = (UILabel *)self;
            NSLog(@"===008 %@ %lf", label.text, label.wb_width);
        }
    }
    if (isnan(nanFrame.size.height)) {
        nanFrame.size.height = 0.f;
        NSLog(@"===004 %lf %lf %lf %lf %@", nanFrame.origin.x, nanFrame.origin.y, nanFrame.size.width, nanFrame.size.height, NSStringFromClass([self class]));
        if ([NSStringFromClass([self class]) isEqualToString:@"UIButton"]) {
            UIButton * button = (UIButton *)self;
            NSLog(@"===009 %@ %lf", button.titleLabel.text, button.wb_width);
        }
        if ([NSStringFromClass([self class]) isEqualToString:@"UILabel"]) {
            UILabel * label = (UILabel *)self;
            NSLog(@"===010 %@ %lf", label.text, label.wb_width);
        }
    }
    return nanFrame;
}

- (void)setWb_x:(CGFloat)x
{
    CGRect frame = self.frame;
    frame.origin.x = x;
    self.frame = frame;
}

- (void)setWb_y:(CGFloat)y
{
    CGRect frame = self.frame;
    frame.origin.y = y;
    self.frame = frame;
}

- (CGFloat)wb_x
{
    return self.frame.origin.x;
}

- (CGFloat)wb_y
{
    return self.frame.origin.y;
}

- (void)setWb_centerX:(CGFloat)centerX
{
    CGPoint center = self.center;
    center.x = centerX;
    self.center = center;
}

- (CGFloat)wb_centerX
{
    return self.center.x;
}

- (void)setWb_centerY:(CGFloat)centerY
{
    CGPoint center = self.center;
    center.y = centerY;
    self.center = center;
}

- (CGFloat)wb_centerY
{
    return self.center.y;
}

- (void)setWb_width:(CGFloat)width
{
    CGRect frame = self.frame;
    frame.size.width = width;
    self.frame = frame;
}

- (void)setWb_height:(CGFloat)height
{
    CGRect frame = self.frame;
    frame.size.height = height;
    self.frame = frame;
}

- (CGFloat)wb_height
{
    return self.frame.size.height;
}

- (CGFloat)wb_width
{
    return self.frame.size.width;
}


- (void)setWb_size:(CGSize)wb_size
{
    CGRect frame = self.frame;
    frame.size = wb_size;
    self.frame = frame;
}

- (CGSize)wb_size
{
    return self.frame.size;
}

- (void)setWb_origin:(CGPoint)origin
{
    CGRect frame = self.frame;
    frame.origin = origin;
    self.frame = frame;
}

- (CGPoint)wb_origin
{
    return self.frame.origin;
}

- (CGFloat)wb_left {
    return self.frame.origin.x;
}

- (void)setWb_left:(CGFloat)x {
    CGRect frame = self.frame;
    frame.origin.x = x;
    self.frame = frame;
}

- (CGFloat)wb_right {
    return self.frame.origin.x + self.frame.size.width;
}

- (void)setWb_right:(CGFloat)right {
    CGRect frame = self.frame;
    frame.origin.x = right - frame.size.width;
    self.frame = frame;
}

- (CGFloat)wb_top {
    return self.frame.origin.y;
}

- (void)setWb_top:(CGFloat)y {
    CGRect frame = self.frame;
    frame.origin.y = y;
    self.frame = frame;
}

- (CGFloat)wb_bottom {
    return self.frame.origin.y + self.frame.size.height;
}

- (void)setWb_bottom:(CGFloat)bottom {
    CGRect frame = self.frame;
    frame.origin.y = bottom - frame.size.height;
    self.frame = frame;
}

- (CGPoint)wb_offsetFromView:(UIView*)otherView {
    CGFloat x = 0, y = 0;
    for (UIView* view = self; view && view != otherView; view = view.superview) {
        x += view.wb_left;
        y += view.wb_top;
    }
    return CGPointMake(x, y);
}

-(void)wb_removeAllSubViews{
    for (UIView *subview in self.subviews){
        [subview removeFromSuperview];
    }
}

- (UIView *)wb_subViewOfClassName:(NSString*)className{
    for (UIView* subView in self.subviews) {
        if ([NSStringFromClass(subView.class) isEqualToString:className]) {
            return subView;
        }
        
        UIView* resultFound = [subView wb_subViewOfClassName:className];
        if (resultFound) {
            return resultFound;
        }
    }
    return nil;
}

///////////////////////////////////////////////////////////////////////////////////////////////////
- (UIView*)wb_descendantOrSelfWithClass:(Class)cls {
    if ([self isKindOfClass:cls])
        return self;
    
    for (UIView* child in self.subviews) {
        UIView* it = [child wb_descendantOrSelfWithClass:cls];
        if (it)
            return it;
    }
    
    return nil;
}


///////////////////////////////////////////////////////////////////////////////////////////////////
- (UIView*)wb_ancestorOrSelfWithClass:(Class)cls {
    if ([self isKindOfClass:cls]) {
        return self;
        
    } else if (self.superview) {
        return [self.superview wb_ancestorOrSelfWithClass:cls];
        
    } else {
        return nil;
    }
}

- (void)wb_addLineOnDirection:(BorderDirectionType)direction color:(UIColor *)color width:(CGFloat)width {
    
    CALayer * border = [CALayer layer];
    border.backgroundColor = color.CGColor;
    switch (direction) {
        case BorderDirectionTop:
            border.frame = CGRectMake(0.f, 0.f, self.wb_width, width);
            break;
        case BorderDirectionLeft:
            border.frame = CGRectMake(0.f, 0.f, width, self.wb_height);
            break;
        case BorderDirectionBottom:
            border.frame = CGRectMake(0.f, self.wb_height - width, self.wb_width, width);
            break;
        case BorderDirectionRight:
            border.frame = CGRectMake(self.wb_width - width, 0.f, width, self.wb_height);
            break;
        default:
            break;
    }
    [self.layer addSublayer:border];
}

- (NSObject *)wb_attachment {
    return objc_getAssociatedObject(self, @"kViewAttachment");
}

- (void)setWb_attachment:(NSObject *)attachment {
    objc_setAssociatedObject(self, @"kViewAttachment",nil, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    objc_setAssociatedObject(self, @"kViewAttachment",attachment, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (NSObject *)wb_attachmentView {
    return objc_getAssociatedObject(self, @"kViewAttachmentView");
}

- (void)setWb_attachmentView:(NSObject *)attachment {
    objc_setAssociatedObject(self, @"kViewAttachmentView",nil, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    objc_setAssociatedObject(self, @"kViewAttachmentView",attachment, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (NSObject *)wb_attachmentIndicatorView {
    return objc_getAssociatedObject(self, @"kViewAttachmentIndicatorView");
}

- (void)setWb_attachmentIndicatorView:(NSObject *)attachment {
    objc_setAssociatedObject(self, @"kViewAttachmentIndicatorView",nil, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    objc_setAssociatedObject(self, @"kViewAttachmentIndicatorView",attachment, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (NSObject *)wb_attachmentLoadingView {
    return objc_getAssociatedObject(self, @"kViewAttachmentLoadingView");
}

- (void)setWb_attachmentLoadingView:(NSObject *)attachment {
    objc_setAssociatedObject(self, @"kViewAttachmentLoadingView",nil, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    objc_setAssociatedObject(self, @"kViewAttachmentLoadingView",attachment, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}


@end


@implementation UIButton (Extension)

/*==== button 设置图文位置 ====*/
- (void)wb_setImagePositionStyle:(OrgImagePositionStyle)imagePositionStyle spacing:(CGFloat)spacing {
    
    if (imagePositionStyle == OrgImagePositionStyleDefault) {
        self.imageEdgeInsets = UIEdgeInsetsMake(0, - 0.5 * spacing, 0, 0.5 * spacing);
        self.titleEdgeInsets = UIEdgeInsetsMake(0, 0.5 * spacing, 0, - 0.5 * spacing);
    } else if (imagePositionStyle == OrgImagePositionStyleRight) {
        CGFloat imageW = self.imageView.image.size.width;
        CGFloat titleW = self.titleLabel.frame.size.width;
        CGFloat imageOffset  = titleW + 0.5 * spacing;
        CGFloat titleOffset  = imageW + 0.5 * spacing;
        self.imageEdgeInsets = UIEdgeInsetsMake(0, imageOffset,   0, - imageOffset);
        self.titleEdgeInsets = UIEdgeInsetsMake(0, - titleOffset, 0, titleOffset);
    } else if (imagePositionStyle == OrgImagePositionStyleTop) {
        CGFloat imageW = self.imageView.frame.size.width;
        CGFloat imageH = self.imageView.frame.size.height;
        CGFloat titleIntrinsicContentSizeW = self.titleLabel.intrinsicContentSize.width;
        CGFloat titleIntrinsicContentSizeH = self.titleLabel.intrinsicContentSize.height;
        self.imageEdgeInsets = UIEdgeInsetsMake(- titleIntrinsicContentSizeH - spacing, 0, 0, - titleIntrinsicContentSizeW);
        self.titleEdgeInsets = UIEdgeInsetsMake(0, - imageW, - imageH - spacing, 0);
    } else if (imagePositionStyle == OrgImagePositionStyleBottom) {
        CGFloat imageW = self.imageView.frame.size.width;
        CGFloat imageH = self.imageView.frame.size.height;
        CGFloat titleIntrinsicContentSizeW = self.titleLabel.intrinsicContentSize.width;
        CGFloat titleIntrinsicContentSizeH = self.titleLabel.intrinsicContentSize.height;
        self.imageEdgeInsets = UIEdgeInsetsMake(titleIntrinsicContentSizeH + spacing, 0, 0, - titleIntrinsicContentSizeW);
        self.titleEdgeInsets = UIEdgeInsetsMake(0, - imageW, imageH + spacing, 0);
    }
}

+ (instancetype)wb_buttonWithFrame:(CGRect)frame
                              title:(NSString *)title
                             target:(id)target
                             method:(SEL)method {
    
    return [self wb_buttonWithFrame:frame
                               title:title
                            fontSize:14.f
                          titleColor:[UIColor blackColor]
                             bgColor:[UIColor whiteColor]
                              target:target
                              method:method];
}

+ (instancetype)wb_buttonWithFrame:(CGRect)frame
                              title:(NSString *)title
                           fontSize:(CGFloat)fontSize
                         titleColor:(UIColor *)titleColor
                            bgColor:(UIColor *)bgColor
                             target:(id)target
                             method:(SEL)method {
    
    return [self wb_buttonWithFrame:frame
                               title:title
                            fontSize:fontSize
                          titleColor:titleColor
                             bgColor:bgColor
                               image:nil
                             bgImage:nil
                              target:target
                              method:method];
}

+ (instancetype)wb_buttonWithFrame:(CGRect)frame
                              title:(NSString *)title
                           fontSize:(CGFloat)fontSize
                         titleColor:(UIColor *)titleColor
                            bgColor:(UIColor *)bgColor
                              image:(NSString *)image
                            bgImage:(NSString *)bgImage
                             target:(id)target
                             method:(SEL)method {
    
    UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = frame;
    button.titleLabel.font = [UIFont systemFontOfSize:fontSize];
    if (bgColor) {
        button.backgroundColor = bgColor;
    }
    if (titleColor) {
        [button setTitleColor:titleColor forState:UIControlStateNormal];
    }
    if (title) {
        [button setTitle:title forState:UIControlStateNormal];
    }
    if (image) {
        [button setImage:[UIImage imageNamed:image] forState:UIControlStateNormal];
    }
    if (bgImage) {
        [button setBackgroundImage:[UIImage imageNamed:bgImage] forState:UIControlStateNormal];
    }
    if (target) {
        [button addTarget:target action:method forControlEvents:UIControlEventTouchUpInside];
    }
    return button;
}

@end
